/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package displayrepositorytree;



import java.util.Collection;
import java.util.Iterator;

import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

/**
 *
 * @author tshepo
 *
 */
public class RetrieveSVNRepository {

   /*
    *TODO: 1.Remove url,name and password
    *      2.Rather be added as args in a command line
    *      3.When logout is up and running
    *      4.Create xml configuaration file to hide sensetive information
    */
    public String url = "https://oomgert.googlecode.com/svn/trunk/";
    public String name = "anonymous";
    public String password = "anonymous";

    public  void setupSVNRepository(){

        
        setupLibrary();// TODO: this should be re-modeld to be a private method
        SVNRepository repository = null;
        try {
            /*
             * Creates an instance of SVNRepository to work with the repository.
             */
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        } catch (SVNException svne) {

            System.err
                    .println("error while creating an SVNRepository for location '"
                            + url + "': " + svne.getMessage());
            System.exit(1);
        }

        /*
         * User's authentication information (name/password) is provided via  an
         * ISVNAuthenticationManager  instance.  SVNWCUtil  creates  a   default
         * authentication manager given user's name and password.

         * TODO: to take user login details use the following
         * authManager = new BasicAuthenticationsManager(userName, userPassword);
         *
         * for now anonymous access will be used.
         */
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
        repository.setAuthenticationManager(authManager);

        try {
            /*
             * Checks up if the specified path to repository part of the URL
             * really corresponds to a directory. If doesn't the program exits.
             *
             * 1.SVNNodeKind is that one who says what is located at a path in a
             *   revision.
             * 2 NOTE:. -1 means the latest revision.
             *        which mean HEAD-revision in  svn 's perspective
             */
            SVNNodeKind nodeKind = repository.checkPath("", -1);
            if (nodeKind == SVNNodeKind.NONE) {
                System.err.println("There is no entry at '" + url + "'.");
                System.exit(1);
            } 

            /*
             * getRepositoryRoot() returns the actual root directory where the
             * repository was created.
             */
            System.out.println("Repository Root: " + repository.getRepositoryRoot(true));
            System.out.println("");

            
              //Displays the repository tree at the current path
             
            listEntries(repository, "");
        } catch (SVNException svne) {
            System.err.println("error while listing entries: "
                    + svne.getMessage());
            System.exit(1);
        }
        /*
         * Gets the latest revision number of the repository
         */
        long latestRevision = -1;
        try {
            latestRevision = repository.getLatestRevision();
        } catch (SVNException svne) {
            System.err
                    .println("error while fetching the latest repository revision: "
                            + svne.getMessage());
            System.exit(1);
        }
        System.out.println("");
        System.out.println("---------------------------------------------");
        System.out.println("Repository latest revision: " + latestRevision);
        System.exit(0);
    }


    /*
     * Initializes the library to work with a repository specified protocols
     */
    private static void setupLibrary() {
       
        DAVRepositoryFactory.setup();

    }

    public static void listEntries(SVNRepository repository, String path)
            throws SVNException {
        /*
         * Gets the contents of the directory specified by path at the latest
         * revision.
         *
         * 1.getDir returns a Collection of SVNDirEntry elements
         * 2.SVNDirEntry represents information about the directory entry.
         * 
         */
        Collection entries = repository.getDir(path, -1, null,
                (Collection) null);
        Iterator iterator = entries.iterator();
        int number = 0;
        while (iterator.hasNext()) {
            number = number + 1;
            SVNDirEntry entry = (SVNDirEntry) iterator.next();
            System.out.println(number +"/" + (path.equals("") ? "" : path + "/")
                    + entry.getName() + " (author: '" + entry.getAuthor()
                    + "'; revision: " + entry.getRevision() + "; date: " + entry.getDate() + ")");
            /*
             * Checking up if the entry is a directory.
             */
            if (entry.getKind() == SVNNodeKind.DIR) {
                listEntries(repository, (path.equals("")) ? entry.getName()
                        : path + "/" + entry.getName());
            }
        }
    }
}
